﻿using System;

namespace StockAccessors.Interdin.Model
{
	internal class OrderExecutionLink
	{
		#region Constants

		public const string Url = "https://www.interdinfuturos.com/Broker/Ops/InsertarModificarOrden.asp";
		private const string MarketRequestParameters = @"CV={0}&IP={1}&YY={2}&M={3}&St={4}&A={5}&IO={6}&DD={7}&nc={8}&DisclaimerTest=&TO={9}&Vol={10}&RKNR={11}&Enviar.x=1&Enviar.y=1&Enviar=enviar&Mail=1";
		private const string LimitedRequestParameters = @"CV={0}&IP={1}&YY={2}&M={3}&St={4}&A={5}&IO={6}&DD={7}&nc={8}&DisclaimerTest=&TO={9}&Vol={10}&PL={11}&RKNR={12}&Enviar.x=1&Enviar.y=1&Enviar=enviar&Mail=1";
		
		#endregion

		#region Properties

		public int ContractNumber { get; private set; }
		public int IP { get; private set; }
		public int Year { get; private set; }
		public int Month { get; private set; }
		public string St { get; private set; }
		public bool BuySell { get; private set; }
		public int Volume { get; private set; }
		public OrderType OrderType { get; private set; }
		public string RKNR { get; private set; }
		public string A { get; private set; }
		public string IO { get; private set; }
		public string DD { get; private set; }
		public decimal? PriceLimit { get; private set; }

		#endregion

		#region Public Methods

		/// Constructs a link to execute market orders
		public OrderExecutionLink(int contractNumber, int ip, int year, int month, string st, bool buySell, int volume, string rknr, string a, string io, string dd)
		{
			this.DD = dd;
			this.IO = io;
			this.A = a;
			this.RKNR = rknr;
			this.OrderType = OrderType.Market;
			this.Volume = volume;
			this.BuySell = buySell;
			this.St = st;
			this.Month = month;
			this.Year = year;
			this.IP = ip;
			this.ContractNumber = contractNumber;
		}

		/// Contructs a link to execute limited orders
		public OrderExecutionLink(int contractNumber, int ip, int year, int month, string st, bool buySell, int volume, decimal priceLimit, string rknr, string a, string io, string dd)
		{
			this.DD = dd;
			this.IO = io;
			this.A = a;
			this.RKNR = rknr;
			this.OrderType = OrderType.Limited;
			this.Volume = volume;
			this.BuySell = buySell;
			this.St = st;
			this.Month = month;
			this.Year = year;
			this.IP = ip;
			this.ContractNumber = contractNumber;
			this.PriceLimit = priceLimit;
		}

		public string GetMarketLinkParameters()
		{
			char orderDirection = this.BuySell ? 'S' : 'B';

			return string.Format(OrderExecutionLink.MarketRequestParameters, orderDirection, this.IP, this.Year, this.Month.ToString("00"), this.St, this.A, this.IO, this.DD, this.ContractNumber, (int)this.OrderType, this.Volume, this.RKNR);
		}

		public string GetLimitedLinkParameters()
		{
			if (this.PriceLimit == null)
			{
				throw new InvalidOperationException("Limited order parameters can only be generated when the constructor with price limit has been used");
			}
			char orderDirection = this.BuySell ? 'S' : 'B';

			return string.Format(OrderExecutionLink.LimitedRequestParameters, orderDirection, this.IP, this.Year, this.Month.ToString("00"), this.St, this.A, this.IO, this.DD, this.ContractNumber, (int)this.OrderType, this.Volume, this.PriceLimit.Value, this.RKNR);
		}

		#endregion
	}
}
